package com.hbsx.why.ui;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.TextView;
import com.hbsx.why.R;
import com.hbsx.why.adapter.LGRecycleViewAdapter;
import com.hbsx.why.adapter.LGViewHolder;
import com.hbsx.why.base.BaseActivity;
import com.hbsx.why.model.bean.UserBean;
import com.hbsx.why.utils.refresh.PullRecycleView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by liub on 2017/7/10 09:50.
 */

public class DemoRecycleViewActivity extends BaseActivity implements PullToRefreshLayout.OnRefreshListener {
    @BindView(R.id.demo_recycleview)
    PullRecycleView demoRecycleview;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout mPullToRefreshLayout;
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.text_top_right)
    TextView text_top_right;
    private Context context = this;
    private List<UserBean> stringList;
    private UserBean userBean;
    private int page = 1;
    private MainAdapter mainAdapter;
    String TAG = "I/TAG";
    private boolean flag = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.demo_recycleview_refresh;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText("demo_recycleview_refresh");
        text_top_right.setText("网格/线性");
        text_top_right.setVisibility(View.VISIBLE);
        demoRecycleview.setPullUpEnable(true);
        demoRecycleview.setPullDownEnable(true);
        demoRecycleview.setNestedScrollingEnabled(false);
        setLinearLayout(demoRecycleview, context);
        mPullToRefreshLayout.setOnRefreshListener(this);
        getData(page);

        mainAdapter = new MainAdapter(stringList);
        mainAdapter.setFlag(flag);
        demoRecycleview.setAdapter(mainAdapter);
        mainAdapter.setOnItemClickListener(R.id.text_view, new LGRecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
                toast_short("TEXT");
            }
        });
        mainAdapter.setOnItemClickListener(R.id.image_view, new LGRecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
                toast_short("IMG");
            }
        });
    }

    @OnClick({R.id.tv_topTitle, R.id.text_top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_topTitle:
                break;
            case R.id.text_top_right:
                if (flag) {
                    flag = false;
                    mainAdapter.setFlag(flag);
                    setLinearLayout(demoRecycleview, context);
                } else {
                    flag = true;
                    mainAdapter.setFlag(flag);
                    setStaggeredGridLayout(demoRecycleview, 2);
                }
                break;
        }
    }

    private void getData(int page) {

        if (page == 1) {
            stringList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                userBean = new UserBean();
                userBean.setAge(20 + i + "");
                userBean.setName("数据刷新--" + page + i);
                userBean.setDesc("" + i);
                userBean.setId(i + "");
                userBean.setItemType(i);
                stringList.add(userBean);
            }
        } else {
            stringList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                userBean = new UserBean();
                userBean.setAge(20 * page + i + "");
                userBean.setName("数据加载--" + page + i);
                userBean.setDesc("" + i);
                userBean.setId(i + "");
                userBean.setItemType(i);
                stringList.add(userBean);
            }
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        pullToRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Toast.makeText(getApplicationContext(), "onRefresh1200", Toast.LENGTH_LONG).show();
                mPullToRefreshLayout.refreshFinish(true);
                page = 1;
                getData(1);
                mainAdapter.refreshDate(stringList);
                mainAdapter.notifyDataSetChanged();
            }
        }, 1200); // 2秒后刷新
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        pullToRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPullToRefreshLayout.loadMoreFinish(true);
                page = page + 1;
                getData(page);
                mainAdapter.addDate(stringList);
                mainAdapter.notifyDataSetChanged();
            }
        }, 500); // 2秒后刷新
    }

    private static class MainAdapter extends LGRecycleViewAdapter<UserBean> {
        private boolean flag=true;

        public boolean isFlag() {
            return flag;
        }

        public void setFlag(boolean flag) {
            this.flag = flag;
        }

        public MainAdapter(List<UserBean> dataList) {
            super(dataList);
        }

        @Override
        public void convert(LGViewHolder holder, UserBean productBean, int position) {
            int itemType;
            itemType = productBean.getItemType() % 2;
            TextView textView = (TextView) holder.getView(R.id.text_view);
            switch (itemType) {
                case 0:
                    textView.setText(productBean.getName());
                    break;
                case 1:
                    textView.setText(productBean.getName());
                    break;
                default:
                    textView.setText(productBean.getName());
                    break;
            }
        }

        @Override
        public int getLayoutId(int viewType) {
            if (flag) {
                switch (viewType) {
                    case 0:
                        viewType = R.layout.item_list_view;
                        break;
                    case 1:
                        viewType = R.layout.item_listrecycle_view;
                        break;
                    default:
                        viewType = R.layout.item_list_view;
                        break;
                }
            }else {
                switch (viewType) {
                    case 0:
                        viewType = R.layout.item_list_view_flag;
                        break;
                    case 1:
                        viewType = R.layout.item_listrecycle_view_flag;
                        break;
                    default:
                        viewType = R.layout.item_list_view_flag;
                        break;
                }
            }

            return viewType;
        }

        //支持不同viewType视图
        @Override
        public int getItemViewType(int position) {
            UserBean model = getItem(position);//实际开发中可以通过model的属性来决定返回viewType类型
            int itemType = model.getItemType() % 2;

            switch (itemType) {
                case 0:
                    itemType = 0;
                    break;
                case 1:
                    itemType = 1;
                    break;
                default:
                    itemType = 0;
                    break;
            }
            return itemType;
        }
    }

    /**
     * 线性布局
     */
    public void setLinearLayout(PullRecycleView pullRecycleView, Context mContext) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(linearLayoutManager);
    }

    /**
     * GridLayoutManager
     */

    public void setGridLayout(PullRecycleView pullRecycleView, Context mContext, int spanCount) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, spanCount);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(gridLayoutManager);
    }

    /**
     * StaggeredGridLayoutManager
     */
    public void setStaggeredGridLayout(PullRecycleView pullRecycleView, int spanCount) {
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(spanCount, LinearLayoutManager.VERTICAL);
        pullRecycleView.setLayoutManager(staggeredGridLayoutManager);
    }
}
